# coding=utf8
from django.template.loader import get_template
from django.template import Template, Context
from django.template.context import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth import authenticate, login as auth_login
from django.views.decorators.csrf import csrf_protect
import datetime
from tussin.news.models import Category, Article
from tussin.ads.models import AdItem
from tussin.accounts.forms import TussinUserCreationForm

def home(request):
    image_headlines = Article.objects.filter(category__name="热门头条",status=2,is_headline=True)[0:4]
    headlines = Article.objects.filter(category__name="热门头条", is_headline=False,status=2)[0:11]
    trends = Article.objects.filter(category__name="趋势", status=2)[:12]
    observation = Article.objects.filter(category__name="观察", status=2)[:12]
    ind_pictures = Article.objects.filter(category__name="行业图片", status=2, is_headline=True)[:6]
    corp_video = Article.objects.filter(category__name="企业视频", status=2)[:18]
    mentors = {'headlines': Article.objects.filter(category__name="导师", is_headline=True, status=2)[:9], 'items': Article.objects.filter(category__name="导师", is_headline=False, status=2)[:28]}
    try:
        links = Article.objects.get(title="友情链接")
    except Article.DoesNotExist:
        raise Http404

    ads = AdItem.objects.filter(ad_block__page__exact='homepage')
    topad1 = ads.filter(ad_block__name="首页顶部通栏广告1")[:1]
    topad2 = ads.filter(ad_block__name="首页顶部通栏广告2")[:1]
    left_banner = ads.filter(ad_block__name="首页左侧横幅广告")[:1]
    bottom_ad = ads.filter(ad_block__name="首页下方通栏广告")[:1]
    right_ads = ads.filter(ad_block__name="首页右侧展示广告")
    title_ad_qushi = ads.filter(ad_block__name="首页标题栏广告-趋势")[:1]
    title_ad_guancha = ads.filter(ad_block__name="首页标题栏广告-观察")[:1]
    title_ad_chuangye = ads.filter(ad_block__name="首页标题栏广告-创业")[:1]
    title_ad_pinpai = ads.filter(ad_block__name="首页标题栏广告-品牌")[:1]
    title_ad_zhichang = ads.filter(ad_block__name="首页标题栏广告-职场")[:1]

    chuangye = Category.objects.filter(parent__name="创业")
    pinpai = Category.objects.filter(parent__name="品牌")
    zhichang = Category.objects.filter(parent__name="职场")
    chuangye_articles = []
    pinpai_articles = []
    zhichang_articles = []
    for child in chuangye:
        cy_headline = Article.objects.filter(category=child,status=2,is_headline=True)[:1]
        if cy_headline.count() > 0:
            chuangye_articles.append({'channel': child, 'headline': cy_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
            #chuangye_articles[child] = {'headline': cy_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}
        else:
            #chuangye_articles[child] = {'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}
            chuangye_articles.append({'channel': child, 'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
    for child in pinpai:
        pp_headline = Article.objects.filter(category=child,status=2,is_headline=True)[:1]
        if pp_headline.count() > 0:
            #pinpai_articles[child] = {'headline': pp_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}
            pinpai_articles.append({'channel': child, 'headline': pp_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
        else:
            #pinpai_articles[child] = {'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}
            pinpai_articles.append({'channel': child, 'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
    for child in zhichang:
        zc_headline = Article.objects.filter(category=child,status=2,is_headline=True)[:1]
        if zc_headline.count() > 0:
            zhichang_articles.append({'channel': child, 'headline': zc_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
            #zhichang_articles[child] = {'headline': zc_headline[0], 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}
        else:
            zhichang_articles.append({'channel': child, 'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]})
            #zhichang_articles[child] = {'headline': None, 'items': Article.objects.filter(category=child, status=2, is_headline=False)[0:10]}

    blocks = {'trends': trends, 'observation': observation, 'chuangye': chuangye_articles, 'pinpai': pinpai_articles, 'zhichang': zhichang_articles}
    ads = {'top_ad1': topad1, 'top_ad2': topad2, 'left_banner': left_banner, 'bottom_ad': bottom_ad, 'right_ads': right_ads, 'title_ad_qushi': title_ad_qushi, 'title_ad_guancha': title_ad_guancha, 'title_ad_chuangye': title_ad_chuangye, 'title_ad_pinpai': title_ad_pinpai, 'title_ad_zhichang': title_ad_zhichang }

    return render_to_response('index.html', {'image_headlines': image_headlines, 'headlines': headlines, 'ind_pics': ind_pictures, 'corp_video': corp_video, 'mentors': mentors, 'links': links, 'blocks': blocks, 'ads': ads}, context_instance=RequestContext(request))

def hello(request):
	headers = request.META.items()
	ua = request.META.get('HTTP_USER_AGENT', 'unknown')
	return HttpResponse("Hello World<br />%s</br>ua: %s" % (type(headers),ua))

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('cur_date.html', {'cur_date': now, 'str': '<h3><a href="#">click me</a></h3>'})

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

def weibo_verification(request):
    return HttpResponse('dab7d07658ce43c55eb822181ea17bd5')
